package com.shanglida.main;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.shanglida.R;
import com.shanglida.base.BaseActivity;
import com.shanglida.main.need.StoreActivity;
import com.shanglida.utils.ShakeListener;
import com.shanglida.utils.ShakeListener.OnShakeListener;

public class TabHome extends BaseActivity {

	// page2的变量
	LinearLayout images_layout;// page2中images的线性布局
	ImageView[] images_point;// 存放布局中的所有image点
	int[] images;// 存放所有图片
	int mCurImage = 1;// 当前的image
	ImageView page_image;// 被换的image
	private ShakeListener mShakeListener = null;
	private Vibrator mVibrator;
	private MediaPlayer player;

	// 摇一摇

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_home);

		page2Event();// 处理page2的事件

		mShakeListener = new ShakeListener(TabHome.this);
		mVibrator = (Vibrator) getApplication().getSystemService(
				VIBRATOR_SERVICE);
		player = MediaPlayer.create(this, R.raw.shake_start);

		// 摇一摇
		mShakeListener.setOnShakeListener(new OnShakeListener() {
			public void onShake() {

				mShakeListener.stop();

				startVibrato(); // 开始 震动
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {

						Intent trys = new Intent(TabHome.this,
								StoreActivity.class);
						startActivity(trys);

						mVibrator.cancel();
						mShakeListener.start();
					}
				}, 2000);
			}
		});

	}

	// page2中变量的定义 与 赋值
	private void page2Event() {
		images_layout = (LinearLayout) findViewById(R.id.main_piont);
		images_point = new ImageView[images_layout.getChildCount()];
		page_image = (ImageView) findViewById(R.id.main_images);
		images = new int[] { R.drawable.adv1, R.drawable.adv3, R.drawable.adv2,
				R.drawable.adv4 };

		setScrollerImagePoint();// 初始化 page2中的 滚动点

		final Handler h = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (msg.what == 0x123) {
					if (mCurImage > images.length - 1) {// 如果大于图片的数目 那么重新开始加载
						mCurImage = 0;
					}
					setScrollerImagePoint();
					page_image.setAnimation(AnimationUtils.loadAnimation(
							TabHome.this, android.R.anim.fade_out));
					page_image.setImageResource(images[mCurImage]);
					page_image.setAnimation(AnimationUtils.loadAnimation(
							TabHome.this, android.R.anim.fade_in));
					mCurImage++;
				}
			}
		};

		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					h.sendEmptyMessage(0x123);
				}
			}
		}, 0, 500);
	}

	/*
	 * 为page2设置滚动点的效果
	 */
	public void setScrollerImagePoint() {
		for (int i = 0; i < images_layout.getChildCount(); i++) {
			images_point[i] = (ImageView) images_layout.getChildAt(i);
			images_point[i].setEnabled(false);
		}
		images_point[mCurImage].setEnabled(true);// 设置可用的点 图片
	}

	public void startVibrato() {

		player.setLooping(false);
		player.start();
		// 定义震动
		mVibrator.vibrate(new long[] { 500, 200, 500, 200 }, -1); // 第一个｛｝里面是节奏数组，
																	// 第二个参数是重复次数，-1为不重复，非-1俄日从pattern的指定下标开始重复
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
